<?php

namespace app\admin\controller\site;

use app\common\controller\Backend;
use app\common\model\Config as ConfigModel;

/**
 * 产品管理
 *
 * @icon fa fa-circle-o
 */
class About extends Backend
{

    /**
     * About模型对象
     * @var \app\admin\model\site\About
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\site\About;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function info()
    {
        if($this->request->isAjax()) {
            $about_name = input('row.about_name');
            $url = input('row.about_url');
            $intro = input('row.about_intro');
            $video = input('row.about_video');
            // 查询是否已存在数据
            $about = ConfigModel::where('name', 'about_name')->where('group', 'about')->find();
            if (!$about) {
                $about = new ConfigModel;
            }
            $about->name = 'about_name';
            $about->group = 'about';
            $about->title = 'about Name';
            $about->tip = '请填写显示名称';
            $about->type = 'string';
            $about->value = $about_name;
            $about->save();

            $about_url = ConfigModel::where('name', 'about_url')->where('group', 'about')->find();
            if (!$about_url) {
                $about_url = new ConfigModel;
            }
            $about_url->name = 'about_url';
            $about_url->group = 'about';
            $about_url->title = 'about Url';
            $about_url->tip = '请填写跳转链接';
            $about_url->type = 'string';
            $about_url->value = $url;
            $about_url->save();

            $about_intro = ConfigModel::where('name', 'about_intro')->where('group', 'about')->find();
            if (!$about_intro) {
                $about_intro = new ConfigModel;
            }
            $about_intro->name = 'about_intro';
            $about_intro->group = 'about';
            $about_intro->title = 'about Intro';
            $about_intro->tip = '请填写产品简介';
            $about_intro->type = 'string';
            $about_intro->value = $intro;
            $about_intro->save();

            $about_video = ConfigModel::where('name', 'about_video')->where('group', 'about')->find();
            if (!$about_video) {
                $about_video = new ConfigModel;
            }
            $about_video->name = 'about_video';
            $about_video->group = 'about';
            $about_video->title = 'about Video';
            $about_video->tip = '请上传介绍视频';
            $about_video->type = 'string';
            $about_video->value = $video;
            $about_video->save();
            $this->success();
        }

        $about = ConfigModel::where('group', 'about')->select();
        $data = [
            'about_name' => ['value' => '' ,'tip' => ''],
            'about_url' => ['value' => '' ,'tip' => ''],
            'about_intro' => ['value' => '' ,'tip' => ''],
            'about_video' => ['value' => '' ,'tip' => ''],
        ];
        if($about){
            foreach ($about as $item){
                $data[$item['name']] = [
                    'value' => $item['value'],
                    'tip' => $item['tip'],
                ];
            }

        }
        $this->view->assign('data', $data);
        return $this->fetch('info');
    }
}
